<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>Tracker</title>
  <link rel="stylesheet" href="/static/layui-v2.5.5/css/layui.css">
  <link rel="stylesheet" href="/static/layui-formSelects-master/dist/formSelects-v4.css">
  <link rel="stylesheet" href="/static/Font-Awesome-master/css/all.css" >
  <link rel="stylesheet" href="https://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />
  <link rel="icon" href="/static/img/default.ico">
<!-- 滚动条样式设置开始 -->
<style type="text/css">
.layui-card::-webkit-scrollbar {
  width: 8px;
  background-color: #eee;
}

.layui-card::-webkit-scrollbar-track {
    background-color: #eee;
}

.layui-card::-webkit-scrollbar-thumb {
    background: #3DB6A4;
}

.layui-card::-webkit-scrollbar-button:start {
    background: url(/static/img/up.png) no-repeat;
    background-size: 8px 8px;
}

.layui-card::-webkit-scrollbar-button:end {
    background: url(/static/img/down.png) no-repeat;
    background-size: 8px 8px;
} 
::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
::-webkit-scrollbar-button,
::-webkit-scrollbar-button:vertical {
  display: none;
}
::-webkit-scrollbar-track,
::-webkit-scrollbar-track:vertical {
  background-color: #eee;
}
::-webkit-scrollbar-track-piece {
  background-color: #eee;
}
::-webkit-scrollbar-thumb,
::-webkit-scrollbar-thumb:vertical {
  margin-right: 10px;
  background-color: #3DB6A4;
}
::-webkit-scrollbar-thumb:hover,
::-webkit-scrollbar-thumb:vertical:hover {
  background-color: #eee;
}
::-webkit-scrollbar-corner,
::-webkit-scrollbar-corner:vertical {
  background-color: #eee;
}
::-webkit-scrollbar-resizer,
::-webkit-scrollbar-resizer:vertical {
  background-color: #eee;
}

</style>
<!-- 滚动条样式设置结束 -->
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
  <div class="layui-header">
    <div class="layui-logo"></div>
    <!-- 头部区域（可配合layui已有的水平导航） -->
    <!-- 顶部部导航栏左侧start -->
    <ul class="layui-nav layui-layout-left">
      <li class="layui-nav-item" id="dynamic" onclick="turn_to_page(this.id)"><a href="#dynamic"><i class="layui-icon layui-icon-location"></i> 实时监控</a></li>
      <li class="layui-nav-item" id="trail" onclick="turn_to_page(this.id)"><a href="#trail"><i class="layui-icon layui-icon-engine"></i> 活动轨迹</a></li>
      <li class="layui-nav-item" id="reports" ><a><i class="layui-icon layui-icon-form"></i> 车务管理</a>
        <dl class="layui-nav-child">
          <dd class="" id="car_board"  onclick="turn_to_page(this.id)"><a href="#car_board">看板</a></dd>
          <dd class="" id="car_manage" onclick="turn_to_page(this.id)"><a href="#car_manage">车辆信息</a></dd>
           <dd class="" id="person_manage" onclick="turn_to_page(this.id)"><a href="#person_manage">人员信息</a></dd>
          <dd class="" id="car_apply"  onclick="turn_to_page(this.id)"><a href="#car_apply">用车申请</a></dd>
          <dd class="" id="car_apply_approve"  onclick="turn_to_page(this.id)"><a href="#car_apply_approve">用车审批</a></dd>
          <!-- <dd class="" id="mission_manage"  onclick="turn_to_page(this.id)"><a href="#alarm_report">任务管理</a></dd> -->
        </dl>
      </li>
      <li class="layui-nav-item" id="reports" ><a><i class="layui-icon layui-icon-chart"></i> 统计报表</a>
        <dl class="layui-nav-child">
          <dd class="" id="device_report"  onclick="turn_to_page(this.id)"><a href="#device_report">设备运行统计</a></dd>
          <!-- <dd class="" id="mileage_report" onclick="turn_to_page(this.id)"><a href="#mileage_report">车辆运行统计</a></dd> -->
          <dd class="" id="alarm_report"  onclick="turn_to_page(this.id)"><a href="#alarm_report">报警统计</a></dd>
          <!-- <dd class="" id="apply_report"  onclick="turn_to_page(this.id)"><a href="#apply_report">用车统计</a></dd> -->
        </dl>
      </li>
      <li class="layui-nav-item" id="device_manage"  onclick="turn_to_page(this.id)"><a href="#device_manage"><i class="layui-icon layui-icon-app"></i> 设备管理</a></li>
      <li class="layui-nav-item" id="reports" ><a><i class="layui-icon layui-icon-auz"></i> 参数设置</a>
        <dl class="layui-nav-child">
          <dd class="" id="efence" onclick="turn_to_page(this.id)"><a href="#efence">电子围栏</a></dd>
          <dd class="" id="polyline" onclick="turn_to_page(this.id)"><a href="#polyline">路线管理</a></dd>
          <!-- <dd class="" id="alarm_set" onclick="turn_to_page(this.id)"><a href="#efence">报警设置</a></dd> -->
        </dl>
      </li>
      
      <li class="layui-nav-item" id="tracker_sys_manage" ><a><i class="layui-icon layui-icon-set"></i> 系统管理</a>
        <dl class="layui-nav-child">
          <dd class="" id="user_manage" onclick="turn_to_page(this.id)"><a href="#user_manage">子用户管理</a></dd>
          <dd class="" id="system_log" onclick="turn_to_page(this.id)"><a href="#system_log">系统日志</a></dd>
        </dl>
      </li>
    </ul>
    <!-- 顶部部导航栏左侧end -->

    <!-- 顶部部导航栏右侧start -->
    <ul class="layui-nav layui-layout-right">
      <li class="layui-nav-item"><a id="get_current_time"></a></li>
      <li class="layui-nav-item" id="alarm_detail_show" onclick="alarm_detail_modal()"><a href="#"><i class="layui-icon layui-icon-notice"></i><span id="alarm_num_show" class="layui-badge"></span></a></li>
      <li class="layui-nav-item">
        <a href="javascript:;" id="username_show">
          <!-- <img src="/static/img/default.png" class="layui-nav-img"> -->
        </a>
         <dl class="layui-nav-child">
          <dd class="user_self_btn"><a href="javascript:;">基本资料</a></dd>
          <dd class="user_password_btn"><a href="javascript:;">修改密码</a></dd>
        </dl>
      </li>
      <li class="layui-nav-item"><a href="/">退出</a></li>
    </ul>
  </div>

  <!-- 顶部部导航栏右侧end -->

  <!-- 侧边栏内容start -->
  <div class="layui-side" id="side1">
    <div class="layui-coutainer" id="dynamic_side_bar">
      <div  style="overflow: auto;height: 165px;">
        <table id="id_tree" class="layui-table"></table>
      </div>
     <!--  <div style="height: 20px;padding: 1px">全部</div> -->
      <div class="layui-row" style="padding: 0px;background-color:#009688;text-align: center;">
        <button id="user_chosed" class="layui-btn layui-btn-xs layui-btn-fluid" onclick="device_status_search('all')">全部</button>
      </div>
      <div class="layui-row" style="padding: 2px;">
    		<button id="dev_online" class="layui-btn layui-btn-xs layui-btn-radius" onclick="device_status_search('online')">在线</button>
    		<button id="dev_sleep" class="layui-btn layui-btn-normal layui-btn-xs layui-btn-radius" onclick="device_status_search('sleep')">休眠</button>
    		<button id="dev_offline" class="layui-btn layui-bg-cyan layui-btn-xs layui-btn-radius" onclick="device_status_search('offline')">离线</button>
    		<button id="dev_alarm" class="layui-btn layui-btn-danger layui-btn-xs layui-btn-radius" onclick="device_status_search('alarm')">报警</button>
      </div>
     <form class="layui-form layui-form-pane" action="" style="width:auto;height: 20px;padding: 2px">
          <div class="layui-form-item">
              <div class="layui-inline">
                  <div class="layui-input-inline">
                      <input type="text" lay-verify="required" name="search_key" lay-reqtext="请输入搜索内容" placeholder="设备ID/名称" autocomplete="off" class="layui-input" style="width:auto;height: 30px">
                  </div>
                  <div class="layui-inline" >
                      <a type="button" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="dev-search-btn" >搜索</a>
                  </div>
              </div>
          </div>
      </form>
      <div class="" id="dev_list_panle">
        <div class="layui-card-body" style="padding: 2px;border: 1px">
          <table id="dev_list_show" lay-filter="dev_list_show"></table>
        </div>
      </div>
    </div>
  </div>
  <div class="" id="side2" style="display: none;"></div>
  <!-- 侧边栏内容end -->
  
  <!-- 内容主体区域sart -->

  <div class="layui-body" style="bottom: 0px">
  </div>

  <!-- 内容主体区域end -->
  <div class="layui-footer" style="display: none;"></div>

</div>

<!-- 实时报警信息弹窗开始 -->

<!-- 单个报警处理按钮 -->
<script type="text/html" id="alarm_table_bar">
    <a class="layui-btn layui-btn-xs" lay-event="excue_alarm">处理</a>
</script>
<form  class="layui-form layui-form-pane " action="" id="alarm_detail_form" lay-filter="alarm_detail_form" style="padding: 10px;display: none;">
  <table class="layui-table" id="alarm_detail_table" lay-size="lg" lay-filter="alarm_detail_table">
  </table>
</form>
<!-- 报警信息批量处理按钮 -->
<script type="text/html" id="alarm_handle_bar">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" type="button" lay-event="alarm_handle">批量处理</button>
    <button class="layui-btn layui-btn-sm" type="button" lay-event="all_alarm_handle">全部处理</button>
  </div>
</script>
<!-- 报警处理备注框 -->
<form  class="layui-form layui-form-pane " action="" id="alarm_handle_mark_form" lay-filter="alarm_handle_mark_form" style="padding: 10px;display: none;">
	<textarea placeholder="请输入报警处理备注" class="layui-textarea" name="alarm_handle_mark"></textarea>
</form>
<!-- 报警声音 -->
<audio loop="loop" id="alarm_audio_play" src="/static/audio/didi.mp3"></audio>

<!-- 实时报警信息弹窗结束 -->

<!-- 登录用户信息编辑start -->
<form  class="layui-form layui-form-pane " action="" id="user_self_form" lay-filter="user_self_form" style="padding: 10px;display: none;">

  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">登录名</label>
      <div class="layui-input-block">
        <input type="text" name="username" lay-verify="required" lay-reqtext="必填项!" placeholder="登录用户名/不可更改" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">显示名</label>
      <div class="layui-input-block">
        <input type="text" name="visible_name" autocomplete="off" placeholder="自定义名称" class="layui-input">
      </div>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">邮箱</label>
      <div class="layui-input-block">
        <input type="text" name="email" placeholder="邮箱"  autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">联系人</label>
      <div class="layui-input-block">
        <input type="text" name="contact" autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">电话</label>
      <div class="layui-input-block">
        <input type="text" name="phone" placeholder="电话"  autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">备注</label>
      <div class="layui-input-block">
        <input type="text" name="remark" autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>

  <div class="layui-form-item" style="display: none;">
    <div class="layui-block" >
      <label class="layui-form-label layui-bg-green">用户图标</label>
      <div class="layui-input-inline">
        <input type="text" name="user_icon" value="/static/img/default.png" autocomplete="off" placeholder="秒" class="layui-input">
      </div>
    </div>
  </div>

  <div class="layui-form-item" >
    <div class="layui-inline" >
      <label class="layui-form-label layui-bg-green">刷新时间</label>
      <div class="layui-input-block">
        <input type="text" name="refresh_time" value="60" autocomplete="off" placeholder="秒" class="layui-input">
      </div>
    </div>
    <div class="layui-inline" style="display: none;">
      <label class="layui-form-label layui-bg-green">权限</label>
      <div class="layui-input-inline">
        <input type="text" name="kind" class="layui-input">
      </div>
    </div>
  </div>
  <div class="layui-form-item" >
    <label class="layui-form-label layui-bg-green">报警声音</label>
    <div class="layui-input-block">
      <input type="checkbox" checked="" name="alarm_audio" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">
    </div>
  </div>

  <div class="layui-form-item" >
    <label class="layui-form-label layui-bg-green">报警弹窗</label>
    <div class="layui-input-block">
      <input type="checkbox" checked="" name="alarm_show" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">
    </div>
  </div>

  <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="user_self_edit_submit" >提交</button>
</form>

<!-- 登录用户信息编辑end -->

<!-- 登录用户密码修改start -->
<div class="layui-coutainer layui-scroll"><form class="layui-form layui-form-pane " action="" id="user_password_form" lay-filter="user_password_form" style="padding: 10px;display: none;">
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">输入旧密码</label>
      <div class="layui-input-block">
        <input type="password" name="old_password" lay-verify="required" autocomplete="off"  class="layui-input">
      </div>
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">输入新密码</label>
      <div class="layui-input-block">
        <input type="password" name="new_password1" lay-verify="required" autocomplete="off"  class="layui-input">
      </div>
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">再次输入新密码</label>
      <div class="layui-input-block">
        <input type="password" name="new_password2" lay-verify="required" autocomplete="off"  class="layui-input">
      </div>
    </div>
  </div>


  <div class="layui-form-item">
    <button class="layui-btn layui-btn-fluid" lay-submit="" lay-filter="password_change_submit">提交</button>
  </div>

</form></div>



<!-- 登录用户密码修改start -->

<!-- 引用layui模块 -->

<script type="text/javascript" src="/static/layui-v2.5.5/layui.all.js"></script>
<script type="text/javascript" src="/static/layui-formSelects-master/dist/formSelects-v4.js"></script>
<!-- 百度api的https引用 -->
<script type="text/javascript">window.HOST_TYPE='2'</script>
<script type="text/javascript" src="https://api.map.baidu.com/getscript?v=2.0&ak=nSxiPohfziUaCuONe4ViUP2N&s=1"></script> 
<!-- <script type="text/javascript" src="http://api.map.baidu.com/library/LuShu/1.2/src/LuShu_min.js"></script> -->
<script type="text/javascript" src="/static/js/efence_tool.js"></script>
<!-- <script type="text/javascript" src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script> -->



<!-- JavaScript代码区域 -->
<script type="text/javascript">

var collect_obj_func=function(d,key){
  var objs={}
  for(var i=0;i<d.length;i++){
    objs[d[i][key]]=d[i]
  }
  return objs
}

var $=layui.jquery;

var device={{device|safe}};
console.log(device)
var dev_model={{dev_model|safe}};
var dev_model_objs=collect_obj_func(dev_model,'unique_id')
var user_tree={{user_tree|safe}};
user_tree.unshift({'name':'all','title':'全部'})
var user_data={{sub_user_detail|safe}};
var user_self={{user_self|safe}};
var character={{character|safe}};
var user_info_objs=collect_obj_func(user_data,'username')
user_info_objs[user_self.username]=user_self
var alarm_data={{alarm_data|safe}};
// 记录当前选中的设备的全局变量
var chosed_dev={'device_id':'default'};

// --用户设置赋值处理开始-------------------

var user_self_detail={}
user_self_detail=$.extend(user_self,character)
// 显示图标
var user_icon=character['user_icon'];
// 报警声音,弹窗，刷新时间
if(character['alarm_audio']==='off'){
  user_self_detail['alarm_audio']=false
}
if(character['alarm_show']==='off'){
  user_self_detail['alarm_show']=false
}
// console.log(user_self_detail)

// 刷新时间,从用户设置当中选择，最小10秒刷新一次，低于10秒或者不是数字，默认60秒刷新
var param_refresh_time_control=parseInt(user_self_detail['refresh_time'])*1000

if(typeof(param_refresh_time_control)!=='number' | param_refresh_time_control<10000){
  param_refresh_time_control=60000;
}

// 用户权限，只能查看，能编辑，能分配，能导入，四个级别
user_permi=user_self['kind']
if(user_permi=='普通用户' | user_permi=='phone_user'){
  $('#user_manage').hide()
}
handle_dev_permi=character['handle_device']

// 右上角的用户显示名称和图标,
var username_show='<img src="'+user_icon+'" class="layui-nav-img">'+user_self_detail['visible_name']
$('#username_show').append(username_show)
// 左上角的显示的系统名称
// $('.layui-logo').text(user_self_detail['sys_name'].slice(0,8))
var sys_name_add='<a><img id="kanban" src="/static/img/logo.png" style="position:absolute;top:-10px;left:0px;width:'+get_width/20+'px"></a>'
$('.layui-logo').append(sys_name_add)

// --用户设置赋值处理结束-------------------

var element = layui.element;
var tree=layui.tree;
var table=layui.table;
var layer=layui.layer;
var form=layui.form;
var upload=layui.upload;
var laydate=layui.laydate;
var slider=layui.slider;
var transfer=layui.transfer;
var formSelects = layui.formSelects;

// 获取屏幕高度和宽度

var get_height=$(window).height()
var get_width=$(window).width()



// 用户基本信息弹出


// 监听 基本资料 按钮，做展示和修改操作处理
$(".user_self_btn").on("click", function () {
  document.getElementById("user_self_form").reset();
  form.val('user_self_form',user_self_detail)
  // form.render();
  layer.open({
    type: 1
    ,title:['登录账户：'+user_self['username'],'font-size:18px;']
    ,area:['650px','450px']
    ,offset:'100px'  
    ,id: 'user_self_data' //防止重复弹出
    ,content:$('#user_self_form')
    ,shade: 0 //不显示遮罩
  });
});

// 监听用户设置表单提交，修改名称，图标等数据
form.on('submit(user_self_edit_submit)', function (data) {  
    // 获取表单数据
  var result=data.field
  if(result.visible_name==''){
  result['visible_name']=result.username
  }
  var user_character={}
  user_character.user_icon=result.user_icon
  user_character.refresh_time=result.refresh_time
  if(result.alarm_audio==null){
    result.alarm_audio='off'
  }
  user_character.alarm_audio=result.alarm_audio
  if(result.alarm_show==null){
    result.alarm_show='off'
  }
  user_character.alarm_show=result.alarm_show
  user_character.contact=result.contact
  user_character.email=result.email

  result['character']=JSON.stringify(user_character)
  delete result.contact
  delete result.email
  delete result.user_icon
  delete result.refresh_time
  delete result.alarm_audio
  delete result.alarm_show
  // console.log(result)
  post_info_ajax(result,'user_manage','edit')
  return false

});


// 安全设置弹出
// 监听 安全设置 按钮，做展示和修改操作处理
$(".user_password_btn").on("click", function () {
      // document.getElementById("user_self_form").reset();
      // form.render();
      layer.open({
          type: 1
          ,title:[user_self['username'],'font-size:18px;']
          // ,area:['400px','500px']
          ,offset:'100px'  
          ,id: 'user_password_data' //防止重复弹出
          ,content:$('#user_password_form')
          ,shade: 0 //不显示遮罩
        });
  });

// 监听安全设置提交表单
form.on('submit(password_change_submit)', function (data) {  
    // 获取表单数据
    var result=data.field
    // console.log(result)
    if(user_self.username=='demo'){
      layer.msg('无权限')
      return false
    }
    post_info_ajax(result,'index','handle_password')
    return false;
});



//---- 实时报警展示/处理开始 ----//

// 报警展示表
var alarm_table_func=function(table_data){
  table.render({
    elem: '#alarm_detail_table',
    data: table_data,
    toolbar: '#alarm_handle_bar' ,
    id:'alarm_detail_table',
    skin:'nob',
    size:'lg',
    height: get_height-200,
    cols: [[
      {type: "checkbox", fixed: "left"},
      {field: 'index_num', title: '序号',width:10,templet:function(d){
        return d.LAY_INDEX
      }},
      {field: 'name', title: '设备名称',width:140,templet:function(d){
        // console.log(d.device_id)
        var dev_name=get_detail_from_id(device,d.device_id)
        // console.log(dev_name)
        return dev_name.user_name
      }},
      {field: 'device_id',width:140, title: '设备ID'},
      {field: 'kind', title: '报警类型',width:120,templet:function(d){
        alarm_kind_dict={'sos_alarm':'SOS报警','l_pres_low':'舒张压低报警','l_pres_hight':'舒张压高报警','h_pres_low':'收缩压低报警','h_pres_hight':'收缩压低报警','speed_alarm':'超速报警',
                         'heart_low':'心率低报警','heart_hight':'心率高报警','circle':'围栏报警','polygon':'围栏报警','rectangle':'围栏报警',
                         'fall_down':'摔倒报警','low_power':'低电量报警','stop_alarm':'停留报警','power_cut':'断电报警','polyline':'路线偏移'}
        return alarm_kind_dict[d.kind]+','+d.content
      }},
      // {field: 'content', title: '报警参数',width:120,templet:function(d){
      //   // if(d.kind=='circle' | d.kind=='polygon' | d.kind=='rectangle' ){
      //   //   var efe_con=eval(d.content)
      //   //   var inout=efe_con[1];
      //   //   var inout_dict={'in':'进入','out':'出'}
      //   //   return inout_dict[inout]+efe_con[0]
      //   // }
      //   return d.content
        
      // }},
      {field: 'location', title: '报警地点',templet:function(d){
        var id_name=d.device_id+'alarm'+d.id.toString()
        var loca='<a id="'+id_name+'" class="layui-btn layui-btn-sm" onclick="alarm_location(\''+d.name+'\')">展示地址</a>'
        // console.log(alarm_addr_obj,id_name)
        if(alarm_addr_obj[id_name]){
          loca=alarm_addr_obj[id_name]
        }
        return loca
      }},
      {field: 's_time',width:120, title: '报警时间'},
      {title: '操作',width:80,templet: '#alarm_table_bar', fixed: "right", align: "center"}
    ]],
    page: true
  });
}

// 报警弹窗
var alarm_detail_modal=function(){
  form.render()
  layer.open({
    type: 1
    ,title:'实时报警信息'
    ,area:['1080px',get_height-80+'px']
    ,offset:'60px'  
    ,id: 'alarm_data' //防止重复弹出
    ,content:$('#alarm_detail_form')
    // ,btn: '关闭'
    ,shade: 0 //不显示遮罩
    ,success:function(layero, index){
       alarm_table_func(alarm_data)
    }
  });
};

// 报警地点转中文地址

var chosed_alarm;
var handle_alarm_loca=function(addr){
  // console.log(addr)
  $('#'+chosed_alarm).text(addr.location)
  alarm_addr_obj[chosed_alarm]=addr.location
}
var alarm_addr_obj={}
var alarm_location=function(alarm_name){
  
  var alarm_detail=get_detail_from_id(alarm_data,alarm_name,'name')
  chosed_alarm=alarm_detail.device_id+'alarm'+alarm_detail.id
  if(alarm_addr_obj[chosed_alarm]==null){
    var track_data,track_type;
    var addr=alarm_detail.location
    // console.log(addr)
    addr=eval('('+addr+')')
    

    if(addr.wifi){
      track_data=JSON.stringify(addr.wifi)
      track_type='wifi'
    }else if(addr.lbs){
      track_data=JSON.stringify(addr.lbs)
      track_type='lbs'
    }else if(addr.satelite){
      var geoc = new BMap.Geocoder(); 
      track_data=eval(addr.satelite)
      var pt=new BMap.Point(track_data[0],track_data[1])
      var location
      geoc.getLocation(pt, function(rs){
      var addComp = rs.address
      if(rs.surroundingPois.length!=0){
        // console.log(rs)
         location=addComp+','+rs.surroundingPois[0]['title']
       }else{
          location=addComp
       }
       // console.log(location)
       $('#'+chosed_alarm).text(location)
       alarm_addr_obj[chosed_alarm]=location
     })
      return false
    }
    var data={'track_data':track_data,'track_type':track_type,'device_id':'123456'}
    post_info_ajax(data,'index','get_wifi_lbs_addr',handle_alarm_loca,'not close')
    
  }else{
    $('#'+chosed_alarm).text(alarm_addr_obj[chosed_alarm])
  }
}

// 报警信息的数量展示,声音和弹窗
var alarm_show_func=function(data){
  alarm_data=data
  var alarm_num=data.length;
  var alarm_remine=user_self_detail['alarm_audio']
  var alarm_table_show=user_self_detail['alarm_show']
  // console.log(alarm_num,alarm_remine)
  $('#alarm_num_show').text(alarm_num)
  if(alarm_num>0 && alarm_remine==true){
    var audio_play=document.getElementById('alarm_audio_play');
    // audio_play.play();
    // setTimeout(function(){},1000)
  };
  if(alarm_num>0 && alarm_table_show==true){
    alarm_detail_modal()
  }
  // alarm_table_func(data)
}

alarm_show_func(alarm_data);

// 报警信息刷新
setInterval(function(){
  get_info_ajax('index','get_alarm',alarm_show_func)
},param_refresh_time_control)


 //监听批量报警处理
table.on('toolbar(alarm_detail_table)', function(obj){
  var check_data = table.checkStatus(obj.config.id).data;
  var alarm_name_list = [];
  if(obj.event=='all_alarm_handle'){
    for(var i=0;i<alarm_data.length;i++){
      alarm_name_list.push(alarm_data[i]['name'])
    }
  }
  if(check_data.length==0 && alarm_name_list.length==0){
    layer.msg('请选择需要处理报警信息')
  }else{
    for(var i=0;i<check_data.length;i++){
      alarm_name_list.push(check_data[i]['name'])
    }
    alarm_handle_ajax(alarm_name_list)
  }
});

// 监听单个报警处理
table.on('tool(alarm_detail_table)',function(obj){
  var alarm_name_list=[obj.data.name]
  alarm_handle_ajax(alarm_name_list)
})

// 报警数据处理
var alarm_handle_ajax=function(alarm_name_list,alarm_handle_mark){
  layer.open({
    type: 1
    ,title:'报警信息处理'+JSON.stringify(alarm_name_list)
    ,area:['300px','300px']
    ,offset:'60px'  
    ,id: 'alarm_handle_mark' //防止重复弹出
    ,content:$('#alarm_handle_mark_form')
    ,btn: ['提交','关闭']
    ,shade: 0 //不显示遮罩
    ,btn1: function(layero,index){
      var alarm_handle_mark=form.val('alarm_handle_mark_form');
      if(alarm_handle_mark['alarm_handle_mark']==''){
        alarm_handle_mark='autocomplete'
      };
      var data={'alarm_name_list':alarm_name_list,'alarm_handle_mark':alarm_handle_mark}
      post_info_ajax(data,'index','handle_alarm',alarm_table_func)
      layer.close(index.selector);
    }
    ,btn2: function(layero,index){
      layer.close(index.selector);
    }
  });
}


// ----实时报警展示/处理结束----//


// 刷新之后默认加载的页面
var web_name = window.location.hash;
if(web_name==null | web_name==''){
  web_name='dynamic'
}else{
  web_name=web_name.slice(1)
}

// ---页面跳转start----//
var interval_func=null;
var turn_to_page=function (page_id) {

  var wait_for_ready = layer.load(1);
  $(function(){
    // console.log('ready go')
    layer.close(wait_for_ready); 
  })
  if(interval_func!=null){
    clearInterval(interval_func)
  }
  // console.log(page_id)
    $('.layui-body').load("/static/web/"+page_id+".html")
    if(page_id=='dynamic'){
      $('.layui-side').show()
      $('.layui-body').attr('style','padding:0px;bottom:0px;top:60px;left:300px')
      $('.layui-footer').hide()
      $('.layui-header').show()
    }else if(page_id=='trail' | page_id=='efence' | page_id=='interact'){
      $('.layui-body').attr('style','padding:0px;bottom:0px;left:0px') 
      $('.layui-side').hide()
      $('.layui-footer').hide()
    }else{
      $('.layui-body').attr('style','padding:10px;bottom:0px;left:0px') 
      $('.layui-side').hide()
      $('.layui-footer').hide()
    }
  }
//console.log(web_name)
turn_to_page(web_name)

// ---页面跳转end----//

// 存储跳转到轨迹回放页面的设备id
var trail_device='';
// 搜索功能
var search_func=function(tagret_data,search_key,target_field1,target_field2){
  // console.log(tagret_data,search_key,target_field1,target_field2)
  result_data=[]
  for(var i=0;i<tagret_data.length;i++){
    var search_re1=tagret_data[i][target_field1].search(search_key)
    var search_re2=tagret_data[i][target_field2].search(search_key)
    if(search_re1!==-1 | search_re2!==-1){
      result_data.push(tagret_data[i])
    }
  }
  return result_data
}

// 监听设备搜索操作
form.on('submit(dev-search-btn)', function (data) {

  var search_key = data.field['search_key'];
  if(search_key!==''){
    // 调用index页面搜索函数
    var search_result=search_func(device,search_key,'user_name','device_id')
    // 表格重载
    device_list_table(search_result)
  }else{
    device_list_table(device)
  }
  return false;
});



// 通过设备id获取设备具体信息
var get_detail_from_id=function(data,device_id,field='device_id'){
  for(var i=0;i<data.length;i++){
    if(data[i][field]==device_id){
      return data[i]
    }
  }
}

// 图片上传功能
var img_upload_func=function(div_id){
  upload.render({
  elem: '#'+div_id
  ,auto: false
  ,choose: function(obj){
    obj.preview(function(index, file, result){
      // result是图片 base64编码
      $('#'+div_id).attr('src',result)
    });
  }
});
}

// 设备单选框填充
var fill_raido_func=function(fill_data,list_id,title,key='device_id'){
  $('#'+list_id).empty()
  if(title=='none'){
    for (var i = fill_data.length - 1; i >= 0; i--) {
      var option='<option>'+fill_data[i][key]+'</option>'
      $('#'+list_id).append(option)
    }
  }else{
    for (var i = fill_data.length - 1; i >= 0; i--) {
      var option='<option value="'+fill_data[i][key]+'">'+fill_data[i][title]+'</option>'

      if(fill_data[i][key]==chosed_dev.device_id){
        option='<option value="'+fill_data[i][key]+'" selected>'+fill_data[i][title]+'</option>'
      }
      $('#'+list_id).append(option)
    }
  }
  
}

// 筛选申请信息
var filter_func=function(data,field,key,method=''){
  var res=[]
  if(method=='不等'){
    for(var i=0;i<data.length;i++){
      if(data[i][field]!==key){
        res.push(data[i])
      }
    }
  }else{
    for(var i=0;i<data.length;i++){
      if(data[i][field]==key){
        res.push(data[i])
      }
    }
  }
  return res
}

// 填充信息
var fill_option=function(fill_data,list_id,key,title=''){
  $('#'+list_id).empty()
  if(title==''){
    for (var i = fill_data.length - 1; i >= 0; i--) {
      var option='<option>'+fill_data[i][key]+'</option>'
      $('#'+list_id).append(option)
    }
  }else{
    for (var i = fill_data.length - 1; i >= 0; i--) {
      var option='<option value="'+fill_data[i][key]+'">'+fill_data[i][title]+'</option>'
      $('#'+list_id).append(option)
    }
  }
}

// 数据加载
var ajax_get_info;
var get_info_ajax=function(url,handle_kind='get_init_data',show_func=''){
  $.ajax({
    type: 'GET',
    url:"/"+url+"/?what="+handle_kind,
    async:true,
    dataType: 'json',
    success: function(res) {
      if (res.code==1000){
        // console.log(res)
        var data=res.data
        // 报警信息定时刷新，影响其他页面对ajax_get_info变量的使用
        if(handle_kind!='get_alarm'){
          ajax_get_info=data
        }
        if(show_func!==''){
          show_func(data)
        }
          // layer.msg(res.msg)
          // location.reload()
      }else{
        layer.msg(res.error)
      }  
    },
    error:function(e){
      console.log(e)
    }
  });
}

// 数据上传后台
var post_info_ajax=function(base_info,url,handle_kind,show_func='',hanlde_page='colse_all'){
  // if(user_self.username=='demo' & handle_kind!='tracking'){
  //   layer.msg('无此权限！')
  //   return false
  // }
  base_info=JSON.stringify(base_info);
  // console.log(base_info)
  $.ajax({
    type: 'POST',
    data:{base_info},
    url:"/"+url+"/?what="+handle_kind,
    async:true,
    dataType: 'json',
    success: function(res) {
      // console.log(res)
      if (res.code==1000){
        var data=res.data
        // console.log(data)
        if(show_func!==''){
          show_func(data)
        }

        if(hanlde_page=='colse_all'){
          layer.closeAll();
        }else if(hanlde_page=='reload'){
          location.reload();
        }
        
        if(res.msg!==''){
          layer.msg(res.msg)
        }
        // location.reload()
      }else{
        // console.log(res)
        var error=JSON.stringify(res.error)
        layer.msg(error)
      }  
    },
  })
}

// 获取当前时间
var statistic_func=''
var r_t=param_refresh_time_control/1000
setInterval(function(){
  if(statistic_func!==''){
    statistic_func()
  }
  r_t=r_t-1
  if(r_t==0){
    r_t=param_refresh_time_control/1000
  }
  $('#get_current_time').text(r_t)
  // laydate.render({
  // elem: '#get_current_time'
  // ,type: 'datetime'
  // ,value:new Date()
  // ,btns: ['clear','now', 'confirm']
  // });
},1000)

</script>
</body>
</html>
